package com.gaofei.controller;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import com.qiniu.util.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.UUID;

/**
 * 七牛云文件上传以及路径返回
 * @author : gaofee
 * @date : 13:03 2021/1/17
 * @码云地址 : https://gitee.com/itgaofee
 *
 * Class.forName(com.gaofei.UploadController)
 */
@Controller(value = "uc")
@CrossOrigin

public class UploadController {



    //官网地址:https://developer.qiniu.com/kodo/1239/java
    @ResponseBody
    @RequestMapping(value="/upload", method= RequestMethod.POST)
    public String upload(MultipartFile image){


        //构造一个带指定 Region 对象的配置类,自动设置区
        Configuration cfg = new Configuration(Region.autoRegion());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        cfg.resumableUploadMaxConcurrentTaskCount = 2;  // 设置分片上传并发，1：采用同步上传；大于1：采用并发上传
//...其他参数参考类注释
//...生成上传凭证，然后准备上传
        String accessKey = "yf2wJ8-E32Nx6XcKaFMfAkckj1uzVYnwa7CH37DX";
        String secretKey = "oDzB2zJwE80Y4qX5-RBHf2bWGdXY19mvEHnDrFVB";
        String bucket = "ggas";
//如果是Windows情况下，格式是 D:\\qiniu\\test.png
        String localFilePath = "/home/qiniu/test.mp4";
//默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = UUID.randomUUID().toString();
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
        try {
            //设置断点续传文件进度保存目录
            FileRecorder fileRecorder = new FileRecorder(localTempDir);
            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
            try {
                Response response = uploadManager.put(IOUtils.toByteArray(image.getInputStream()), key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
                return "http://r3d5vestu.hb-bkt.clouddn.com/"+key;
            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }


    @ResponseBody
    @RequestMapping(value="/test11", method= RequestMethod.GET)
    public String upload(){
        return "111";
    }

   /* @ResponseBody
    @RequestMapping(value="/uploadImg", method= RequestMethod.POST)
    public String uploadImg( MultipartFile image, HttpServletRequest request) {
        if (image!=null&&image.isEmpty()) {

            return "文件为空，请重新上传";
        }

        try {
            byte[] bytes = image.getBytes();
            String imageName = UUID.randomUUID().toString();

            QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
            try {
                //使用base64方式上传到七牛云
                String url = qiniuUtil.put64image(bytes, imageName);
                System.out.println(url);

                //发送一个邮件通知用户
//                开启一个线程
                //mailUtil.send(79527743@qq.com);
                //线程结束


                return url;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "啥都没有";
        } catch (IOException e) {

            return "出异常了";
        }
    }
*/
}
